<?php

namespace plugin\tmadmin\app\model;

use DateTimeInterface;
use support\Cache;
use support\Model;


class Base extends Model
{
    /**
     * @var string
     */
    protected $connection = 'plugin.tmadmin.mysql';

    /**
     * 格式化日期
     *
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @key string
     * @value array
     */
    public function cacheSet($key, array $value = [], $expire = 7200)
    {
        if (!empty($key) && !empty($value)) {
            Cache::set("{$this->table}_{$key}", $value, $expire);
        }
        return;
    }
    public function cacheGet($key)
    {
        if (!empty($key) ) {
            return Cache::get("{$this->table}_{$key}");
        }
        return;
    }

    public function cacheClear($key = null)
    {
        if ($key) {
            Cache::delete("{$this->table}_{$key}");
        } else {
            Cache::clear();
        }
        return;
    }
}
